package com.gitee.sop.layui.adminserver.module.content.pay.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gitee.sop.layui.adminserver.module.base.constants.Result;
import com.gitee.sop.layui.adminserver.module.content.pay.api.param.PayConfigParam;
import com.gitee.sop.layui.adminserver.module.content.pay.entity.PayConfig;
import com.gitee.sop.layui.adminserver.module.content.pay.service.PayConfigService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author: Admin
 * @date: 2021/4/2
 * @description:
 * @copyright: 重庆万蓝兴科技有限公司
 */
@RestController
public class PayConfigApi {

    @Resource
    private PayConfigService payConfigService;

    @GetMapping("/api/pay.config.getAllTemplate")
    public Result getAllTemplate() {
        List<Map<String, Object>> allPayConfigTemplate = payConfigService.getAllPayConfigTemplate();
        return Result.success(allPayConfigTemplate);
    }

    @GetMapping("/api/pay.config.getOneTemplate")
    public Result getOnePayConfigTemplate(@RequestParam String code) {
        Map<String, Object> onePayConfigTemplate = payConfigService.getOnePayConfigTemplate(code);
        return Result.success(onePayConfigTemplate);
    }

    @GetMapping("/api/pay.config.page")
    public Result page(PayConfigParam param) {
        Page<PayConfig> page = payConfigService.queryPage(param);
        return Result.successPage(page.getRecords(), page.getTotal(), page.getCurrent(), page.getSize());
    }

    @GetMapping("/api/pay.config.get")
    public Result get(@RequestParam Long id) {
        PayConfig config = payConfigService.getById(id);
        return Result.success(config);
    }

    @PostMapping("/api/pay.config.add")
    @Transactional(rollbackFor = Exception.class)
    public Result add(PayConfig payConfig) {
        payConfigService.addPayConfig(payConfig);
        return Result.success();
    }

    @PostMapping("/api/pay.config.update")
    @Transactional(rollbackFor = Exception.class)
    public Result update(PayConfig payConfig) {
        payConfigService.updatePayConfig(payConfig);
        return Result.success();
    }

    @PostMapping("/api/pay.config.delete")
    @Transactional(rollbackFor = Exception.class)
    public Result delete(@RequestParam Long id) {
        payConfigService.removeById(id);
        return Result.success();
    }

}
